/*
 * RenameDialog.java
 *
 * Created on 2 February, 2009, 4:49 PM
 */

package net.sf.bluex.explorer.boundary;

import java.io.File;
import net.sf.bluex.controller.UsefulMethods;

/**
 *
 * @author  Blue
 */
public class RenameDialog extends javax.swing.JDialog {

    private File file, renamedFile;

    /** Creates new form RenameDialog */
    private RenameDialog(java.awt.Frame parent, File file) {
        super(parent, true);
        initComponents();
        this.file=file;
        initFurther();
        this.setLocationRelativeTo(parent);
        this.setIconImage(net.sf.bluex.controller.UsefulMethods.getImage(net.sf.bluex.controller.FileModule.APP_ICON));
        this.setVisible(true);
    }

    public static File showRenamer(java.awt.Frame parent, File file){
        RenameDialog rd=new RenameDialog(parent, file);
        return rd.renamedFile;
    }
    
    /**
     * private methods
     */
    private void initFurther(){
        //frame init
        this.setLocation(50,50);
        this.setTitle("Rename: "+file.getName());
//        this.getRootPane().setDefaultButton(cmdRename);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent we){
                doCancelJob();
            }
        });
        
        //text box init
        this.txtRename.setText(file.getName());
        this.txtRename.selectAll();
        
        doKeyRealseJob();
    }
    
    private void doRenameJob(){
        renamedFile=new File(this.file.getParentFile(),txtRename.getText());
        if(!this.file.renameTo(renamedFile))
            renamedFile=null;
        doCancelJob();
    }
    
    private void doCancelJob(){
        this.dispose();
    }
    
    private void doKeyRealseJob(){
        if(txtRename.getText().equals("")){
            lbSummary.setText("Enter valid name.");
            cmdRename.setEnabled(false);
        }
        else{
            java.io.File tempFile=new java.io.File(file.getParentFile(), txtRename.getText());
            boolean found=false;
            
            if(file.isFile())
                found=UsefulMethods.fileExists(file.getParentFile(), tempFile, UsefulMethods.FILE_MODE);
            else
                found=UsefulMethods.fileExists(file.getParentFile(), tempFile, UsefulMethods.FOLDER_MODE);
            
            if(found){
                lbSummary.setText("This File/Folder name is already available.");
                cmdRename.setEnabled(false);
            }
            else{
                lbSummary.setText("");
                cmdRename.setEnabled(true);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtRename = new javax.swing.JTextField();
        cmdCancel = new javax.swing.JButton();
        cmdRename = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        lbSummary = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        jLabel1.setText("Rename To:");

        txtRename.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtRenameKeyReleased(evt);
            }
        });

        cmdCancel.setText("Cancel");
        cmdCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelActionPerformed(evt);
            }
        });

        cmdRename.setText("Rename");
        cmdRename.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdRenameActionPerformed(evt);
            }
        });

        jLabel2.setText("Summary:");

        lbSummary.setForeground(java.awt.Color.red);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(cmdRename)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdCancel))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbSummary, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                            .addComponent(txtRename, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmdCancel, cmdRename});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtRename, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbSummary, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdCancel)
                    .addComponent(cmdRename))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void cmdRenameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdRenameActionPerformed
// TODO add your handling code here:
    doRenameJob();
}//GEN-LAST:event_cmdRenameActionPerformed

private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelActionPerformed
// TODO add your handling code here:
    doCancelJob();
}//GEN-LAST:event_cmdCancelActionPerformed

private void txtRenameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRenameKeyReleased
// TODO add your handling code here:
    doKeyRealseJob();
}//GEN-LAST:event_txtRenameKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdCancel;
    private javax.swing.JButton cmdRename;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lbSummary;
    private javax.swing.JTextField txtRename;
    // End of variables declaration//GEN-END:variables

}
